package com.tenllo.util;


import com.tenllo.bean.UserInfo;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.Statement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbConnection {

    public static UserInfo register(HttpServletRequest request, UserInfo userInfo) {
        /* 协议：子协议://主机:端口/数据库名 */
        String url = "jdbc:mysql://localhost:3306/wish";

// mysql数据库的用户名与密码，安装时自己设置，一般默认为root
        String user = "root";
        String password = "lina";

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // 1.加载驱动//com.mysql.jdbc.Driver
            /*
             * DriverManager.registerDriver(new
             * Driver());用这种方法会加载两次驱动，也就是说会创建两个drive对象
             */
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接
            connection = DriverManager.getConnection(url, user, password);

            // 3.获取用于向数据库发送SQL的Statement对象
            statement = connection.createStatement();

            userInfo = UserInfo.Request2User(request, userInfo);

            if (!StringUtils.isEmpty(userInfo.getNick_name())) {
                String sql = "SELECT * FROM user_info;";
                // 4.执行sql,获取数据
                resultSet = statement.executeQuery(sql);

                while (resultSet.next()) {
                    String nickName = resultSet.getString("nick_name");
                    if(nickName.equals(userInfo.getNick_name())) {
                        userInfo.setId(resultSet.getInt("id"));
                        userInfo.setRole(resultSet.getInt("role"));
                        userInfo.setStatus(resultSet.getInt("status"));
                        userInfo.setApptype(resultSet.getInt("apptype"));
                        userInfo.setPhoto(resultSet.getString("photo"));
                        userInfo.setPhone(resultSet.getString("phone"));
                        userInfo.setGender(resultSet.getString("gender"));
                        userInfo.setBirthday(resultSet.getString("birthday"));
                        userInfo.setUser_name(resultSet.getString("user_name"));
                        userInfo.setNick_name(resultSet.getString("nick_name"));
                        userInfo.setAccess_token(resultSet.getString("access_token"));
                        userInfo.setRegister_time(resultSet.getString("register_time"));
                    }
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            //5.关闭连接，释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                resultSet = null;
            }

            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                statement = null;
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                connection = null;
            }
        }
        return userInfo;
    }
}
